package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.DoctorMapper;
import com.ten.mall.mapper.UserMapper;
import com.ten.mall.service.DoctorService;
import com.ten.mall.vo.Doctor;
import com.ten.mall.vo.DoctorExample;
import com.ten.mall.vo.User;
import com.ten.mall.vo.DoctorExample.Criteria;

@Service
@Transactional
public class DoctorServiceImpl implements DoctorService{
	@Autowired
	private DoctorMapper doctorMapper;
	@Autowired
	private UserMapper UserMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> selectPihao(Integer start, Integer size, String name) {
		PageHelper.startPage(start, size);
		DoctorExample example =new DoctorExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNoneBlank(name)){
			c.andDoctorNameLike("%"+name+"%");
		}
		List<Doctor> list =doctorMapper.selectByExample(example);
		PageInfo<Doctor> info =new PageInfo<>(list);
		Map<String, Object> map =new HashMap<>();
		map.put("list", list);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	public int insertPihao(Doctor doctor) {
		int i =doctorMapper.insert(doctor);
		return i;
	}

	@Override
	public int updatePihao(Doctor doctor) {
		int i =doctorMapper.updateByPrimaryKeySelective(doctor);
		if(StringUtils.isNotBlank(doctor.getDoctorRemark())){
			User user =new User();
			user.setId(doctor.getId());
			user.setRemark(doctor.getDoctorRemark());
			UserMapper.updateByPrimaryKeySelective(user);
		}
		return i;
	}

	@Override
	public int deletePihao(String id) {
		int i =doctorMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public Doctor selectById(String id) {
		Doctor doctor =doctorMapper.selectByPrimaryKey(id);
		return doctor;
	}

	

}
